﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web.Mvc;
using System.Web.Mvc.Html;

namespace Mod.CookieDetector.HtmlHelpers
{
    public static class RadioButtonImageListHelper
    {
        //public static string RadioButtonImageList<T>(this HtmlHelper helper, string name, IEnumerable<string> items)
        //{
        //    var selectList = new SelectList(items);
        //    return helper.RadioButtonImageList(name, selectList.);
        //}

        public static MvcHtmlString RadioButtonImageList<TModel, TProperty>(this HtmlHelper<TModel> helper, 
            Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> items,
            string width, string height)
        {
            var metaData = ModelMetadata.FromLambdaExpression(expression, helper.ViewData);
            //var sb = new StringBuilder();
            TagBuilder divTag = new TagBuilder("div");

            foreach (var item in items)
            {
                var id = string.Format("{0}_{1}", metaData.PropertyName, item.Value);
                TagBuilder aTag = new TagBuilder("a");
                aTag.MergeAttribute("class", item.Text);
                aTag.MergeAttribute("href", item.Value);

                
                TagBuilder imgTag = new TagBuilder("img");
                imgTag.MergeAttribute("src", item.Value);
                imgTag.MergeAttribute("width", width);
                imgTag.MergeAttribute("height", height);
                
                var radioTag = helper.RadioButtonFor(expression, item.Value, new { id = id }).ToHtmlString();
                divTag.InnerHtml += radioTag;
                aTag.InnerHtml += imgTag;
                divTag.InnerHtml += aTag;
            }
            return MvcHtmlString.Create(divTag.ToString());
        }
    }
}